package com.demo.easy.oneLine;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFont;

import java.util.List;

/**
 * @Author:YuXuePeng
 * @Date:Created in 2023/4/26 14:12
 */
public class IndexRedWriterHandle216 implements CellWriteHandler {
    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
        if (!aBoolean) { // 不处理表头
//            if (cell.getColumnIndex() == 2) { // 第三列为数字类型
            if (cell.getCellTypeEnum() == CellType.NUMERIC) { // 第三列为数字类型
                CellStyle cellStyle = writeSheetHolder.getSheet().getWorkbook().createCellStyle();
                Font font = writeSheetHolder.getSheet().getWorkbook().createFont();
                double numericCellValue = cell.getNumericCellValue();
                int count = (int) numericCellValue;
                System.out.println(count);
                if (count < 0) {
                    System.out.println(count < 0);
                    font.setColor(IndexedColors.RED.getIndex());
                    cellStyle.setFont(font);
                }
                cell.setCellStyle(cellStyle);
            }
        }
    }
}
